<?php
class News {
	
	/**
     * @var object Zend DB Adapter
     */
	private $_db;
    /**
     * @var string
     */

	public function __construct()
    {
        $this->_db = Zend_Registry::get('db');
    }
    
    public function listNews()
    {
    	$sql = "SELECT * FROM aml_news";
    	$result = $this->_db->fetchAll($sql);
    	
    	return $result;
    }
    
    public function getMainDetails()
    {
    	$sql = "SELECT news_id, news_title, news_desc
    			FROM aml_news
    			LIMIT 0,1";
    	$result = $this->_db->fetchRow($sql);
    	
    	return $result;
    }
	
	public function addNews($form_values)
	{
		$news_subtitle = addslashes($form_values['news_subtitle']);
		$news_sub_desc = addslashes($form_values['news_sub_desc']);
		$news_status = $form_values['status'];
		$news_type = $form_values['type'];
		if($news_status == "")
		{
			$news_status = 0;
		}
		
		$sql = "INSERT INTO aml_news(news_subtitle, news_sub_desc, created_date, status, type)
				VALUES('" . $news_subtitle . "', '" . $news_sub_desc . "', NOW(), " . $news_status . ", '" . $news_type . "')";
		$add_news = $this->_db->exec($sql);		
	}
	
	public function getNewsById($news_id)
	{
		$sql = "SELECT news_id, news_subtitle, news_sub_desc, created_date, status, type 
				FROM aml_news
				WHERE news_id = " . $news_id;
		$result = $this->_db->fetchRow($sql);
    	
    	return $result;
	}
	
	public function updateNews($form_values)
	{
		$news_id = $form_values['news_id'];
		$news_subtitle = addslashes($form_values['news_subtitle']);
		$news_sub_desc = addslashes($form_values['news_sub_desc']);
		$news_status = $form_values['status'];
		$news_type = $form_values['type'];
		if($news_status == "")
		{
			$news_status = 0;
		}
		
		$sql = "UPDATE aml_news
				SET news_subtitle = '" . $news_subtitle . "', news_sub_desc = '" . $news_sub_desc . "', created_date = NOW(), status = " . $news_status . "
				WHERE news_id = " . $news_id; 
		$this->_db->exec($sql);
	}
	
	public function changeNewsStatus($news_id)
	{
		$sql_getstatus = "SELECT status 
				FROM aml_news
				WHERE news_id = " . $news_id;
		$result = $this->_db->fetchRow($sql_getstatus);
		$status = $result['status'];
		
		if($status == 1) {
			$change_status = 0;
		} else {
			$change_status = 1;
		}
		
		$sql_updatestatus = "UPDATE aml_news
							 SET status = " . $change_status . "
							 WHERE news_id = " . $news_id;
		$this->_db->exec($sql_updatestatus);	
	}
	
	public function deleteNews($news_id)
	{
		$sql = "DELETE FROM aml_news
				WHERE news_id = " . $news_id;
		$edit_news = $this->_db->exec($sql);
	}
	
	public function addDefaultContent($form_values)
	{
		$news_title = $form_values['news_title'];
		$news_desc = $form_values['news_desc'];
		$sql = "UPDATE aml_news 
				SET news_title = '" . $news_title . "', news_desc = '" . $news_desc . "' ";
		$update = $this->_db->exec($sql);
	}
}
?>